﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Business.Models;
using Website.Models;

namespace Website.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            if (Session["User"] != null)
            {
                return RedirectToAction("Dashboard", "Home");
            }
            return View();
        }

        [HttpPost]
        public ActionResult Login(String UserName, String Password, byte UserType)
        {
            Object a = null;
            switch (UserType)
            {
                case (byte)CurrentUser.USER_TYPE.ADMIN:
                    a = new Business.DAO.AdministratorsDAO().Login(UserName, Password);
                    break;
                case (byte)CurrentUser.USER_TYPE.RESELLER:
                    a = new Business.DAO.DaiLyDAO().Login(UserName, Password);
                    break;
                case (byte)CurrentUser.USER_TYPE.EMPLOYEE:
                    a = new Business.DAO.QuanTriDAO().Login(UserName, Password);
                    break;
                case (byte)CurrentUser.USER_TYPE.TEACHER:
                    a = new Business.DAO.GiaoVienDAO().Login(UserName, Password);
                    break;
            }

            if (a != null)
            {
                CurrentUser user = new CurrentUser() { UserEntity = a, Name = UserName, UserType = UserType };
                Session["User"] = user;
                return RedirectToAction("Dashboard");
            }
            else
            { }
            return View("Index");
        }

        public ActionResult LogOut()
        {
            Session.Abandon();
            return RedirectToAction("Index", "Home"); 
        }

        public ActionResult Dashboard()
        {
            return View();
        }
    }
}
